home *** CD-ROM | disk | FTP | other *** search
- /*
- * DBMSsetup.c
- *
- * Data source setup dialog, and its associated functions.
- *
- * (c) Apple Computer, Inc 1993
- */
-
- #include <Controls.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include <Lists.h>
- #include <Menus.h>
- #include <OSUtils.h>
- #include <Quickdraw.h>
- #include <String.h>
- #include <Strings.h>
- #include <TextEdit.h>
- #include <Types.h>
- #include <Windows.h>
-
- #include "DialogUtilities.h"
- #include "ODBCINST.H"
- #include "ODBCdbmsSetup.h"
-
- #include "DBMSsetup.h"
-
- /*
- * Defines
- */
-
- #define kDBMSsetupDlogID 134
-
- #define iOKButton 1
- #define iCancelButton 2
- #define iDataSourceNameLabel 3
- #define iDescriptionLabel 4
- #define iOtherLabel 5
- #define iDataSourceName 6
- #define iDescription 7
- #define iOther 8
- #define iTranslationLabel 9
- #define iTranslationName 10
- #define iSelectButton 11
- #define iLine1 12
- #define iLine2 13
-
- /*
- * Structures
- */
-
- typedef struct
- {
- GrafPtr savePort;
- DialogPtr dialog;
- DataSourceInfo *info;
- }
- DoDBMSsetupRec;
-
- /*
- * Globals
- */
-
- static DoDBMSsetupRec gDoDBMSsetupRec;
- static char gTranslate[256];
- static char gTranslateOption[256];
-
- /*
- * Prototypes
- */
-
- Boolean Initialize (DataSourceInfo *info);
- void Cleanup ();
- Boolean DoOkButton ();
- void DoDataSourceName ();
- void DoDescription ();
- void DoOther ();
- void DoSelectButton ();
- void AdjustItems ();
- Boolean GetTranslator (DialogPtr dialog, char *dataSourceName, char *name, long nameMax, long *nameSize, DWORD* option);
- void NumToHexString (long num, char *ptr, long n);
- void HexStringToNum (char *ptr, long *num);
-
- /*
- * Public functions
- */
-
- Boolean
- DoDBMSsetup(WindowPtr parentWindow, DataSourceInfo *info)
- {
- #pragma unused ( parentWindow )
- /*
- * Display the data source setup dialog, centered over parentWindow, and
- * handle events until the user dismisses the dialog.
- */
-
- Boolean result;
- Boolean done = false;
- short item;
-
- if (!Initialize(info)) return false;
-
- while (!done)
- {
- MovableDialog((ModalFilterProcPtr) StandardFilter, &item);
- switch (item)
- {
- case iOKButton:
- done = result = DoOkButton();
- break;
-
- case iCancelButton:
- result = false;
- done = true;
- break;
-
- case iDataSourceName:
- DoDataSourceName();
- break;
-
- case iDescription:
- DoDescription();
- break;
-
- case iOther:
- DoOther();
- break;
-
- case iSelectButton:
- DoSelectButton();
- break;
- }
- }
-
- Cleanup();
-
- return result;
- }
-
- /*
- * Private functions
- */
-
- static Boolean
- Initialize(DataSourceInfo *info)
- {
- /*
- * Create the dialog and initialize its items - return false if anything fails.
- */
-
- DialogPtr dialog = NULL;
- GrafPtr savePort = NULL;
-
- GetPort(&savePort);
- InitCursor();
- dialog = GetNewDialog(kDBMSsetupDlogID, nil, (WindowPtr) -1L);
- if (dialog == NULL) goto bail;
- SetPort(dialog);
-
- SetDUserItem(dialog, iLine1, (ProcPtr) DrawItemLineGray);
- SetDUserItem(dialog, iLine2, (ProcPtr) DrawItemLineGray);
-
- c2pstr(info->name);
- SetDText(dialog, iDataSourceName, info->name);
- SetWTitle((WindowPtr) dialog, info->name);
- p2cstr(info->name);
-
- c2pstr(info->description);
- SetDText(dialog, iDescription, info->description);
- p2cstr(info->description);
-
- c2pstr(info->other);
- SetDText(dialog, iOther, info->other);
- p2cstr(info->other);
-
- strcpy(gTranslate, info->translate);
- strcpy(gTranslateOption, info->translateOption);
-
- gDoDBMSsetupRec.savePort = savePort;
- gDoDBMSsetupRec.dialog = dialog;
- gDoDBMSsetupRec.info = info;
-
- AdjustItems();
- ShowWindow(dialog);
-
- return true;
-
- bail:
-
- if (dialog != NULL) DisposDialog(dialog);
- SetPort(savePort);
-
- return false;
- }
-
- static void
- Cleanup()
- {
- /*
- * Dispose of the dialog and its assundry allocations
- */
-
- DisposDialog(gDoDBMSsetupRec.dialog);
- SetPort(gDoDBMSsetupRec.savePort);
- }
-
- static Boolean
- DoOkButton()
- {
- /*
- * Copy the dialog settings to the DataSourceInfo structure
- */
-
- GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, gDoDBMSsetupRec.info->name);
- p2cstr(gDoDBMSsetupRec.info->name);
-
- GetDText(gDoDBMSsetupRec.dialog, iDescription, gDoDBMSsetupRec.info->description);
- p2cstr(gDoDBMSsetupRec.info->description);
-
- GetDText(gDoDBMSsetupRec.dialog, iOther, gDoDBMSsetupRec.info->other);
- p2cstr(gDoDBMSsetupRec.info->other);
-
- strcpy(gDoDBMSsetupRec.info->translate, gTranslate);
- strcpy(gDoDBMSsetupRec.info->translateOption, gTranslateOption);
-
- return true;
- }
-
- static void
- DoDataSourceName()
- {
- /*
- * Handle changes to the "name" edit field
- */
-
- AdjustItems();
- }
-
- static void
- DoDescription()
- {
- /*
- * Handle changes to the "description" edit field
- */
-
- AdjustItems();
- }
-
- static void
- DoOther()
- {
- /*
- * Handle changes to the "other" edit field
- */
-
- ;
- }
-
- static void
- DoSelectButton()
- {
- /*
- * Select a translator
- */
-
- char translate[256];
- long len;
- DWORD option;
- long optionLong;
- Boolean success;
-
- HexStringToNum(gTranslateOption, &optionLong);
- option = (DWORD) optionLong;
-
- HideWindow(gDoDBMSsetupRec.dialog);
- success = GetTranslator(gDoDBMSsetupRec.dialog, gDoDBMSsetupRec.info->name, translate, 256, &len, &option);
- ShowWindow(gDoDBMSsetupRec.dialog);
-
- if (success)
- {
- strcpy(gTranslate, translate);
- NumToHexString(option, gTranslateOption, 8);
-
- AdjustItems();
- }
- }
-
- static void
- AdjustItems()
- {
- /*
- * Enable/disable dialog items as appropriate
- */
-
- char name[256];
-
- GetDText(gDoDBMSsetupRec.dialog, iDataSourceName, name);
- EnableDItem(gDoDBMSsetupRec.dialog, iOKButton, (name[0] > 0));
-
- c2pstr(gTranslate);
- SetDText(gDoDBMSsetupRec.dialog, iTranslationName, gTranslate);
- p2cstr(gTranslate);
- }
-
- static Boolean
- GetTranslator(DialogPtr dialog, char *dataSourceName, char *name, long nameMax, long *nameSize, DWORD* option)
- {
- /*
- * Get the translator name and option value. Write them out to the ODBC prefs file.
- */
-
- Boolean success;
- UINT size;
-
- size = nameSize;
- success = (Boolean) SQLGetTranslator(dialog, name, nameMax, &size, NULL, 0, NULL, option);
- *nameSize = size;
-
- return success;
- }
-
- static void
- NumToHexString(long num, char *str, long n)
- {
- char *digits = "0123456789ABCDEF";
-
- while (n-- > 0)
- *str++ = digits[(num >> (4*n)) & 0x000F];
-
- *str = '\0';
- }
-
- static void
- HexStringToNum(char *str, long *num)
- {
- char *p = str, ch;
- long v = 0;
-
- while (*p == ' ' || *p == '\t') p++;
-
- for (;;)
- {
- if (! (ch = *p++)) { p--; break; }
-
- if (ch >= '0' && ch <= '9') { v <<= 4; v += ch - '0'; }
- else if (ch >= 'a' && ch <= 'f') { v <<= 4; v += ch - 'a' + 10; }
- else if (ch >= 'A' && ch <= 'F') { v <<= 4; v += ch - 'A' + 10; }
- else break;
- }
-
- *num = v;
- }
-